package com.whmnx.home.ui.model;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;

import com.whmnx.base.constants.EventConstants;
import com.whmnx.home.BR;
import com.whmnx.home.R;
import com.whmnx.home.ui.adapter.ViewPagerHomeBindingAdapter;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.binding.command.BindingConsumer;
import me.goldze.mvvmhabit.bus.RxBus;
import me.goldze.mvvmhabit.utils.ToastUtils;
import me.tatarka.bindingcollectionadapter2.BindingViewPagerAdapter;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

/**
 * author : huang
 * date   : 2020/9/25 0025 16:41
 * version: 1.0
 * @author Administrator
 */
public class HomeModel extends BaseViewModel {

    public HomeModel(@NonNull Application application) {
        super(application);
    }


    //给ViewPager添加ObservableList
    public ObservableList<ViewPagerHomeItemViewModel> items = new ObservableArrayList<>();

    //给ViewPager添加ItemBinding
    public ItemBinding<ViewPagerHomeItemViewModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_viewpager_home);


    private String[] titles = new String[]{"关注","推荐","直播"};



    public void addPage() {
        items.clear();
        //模拟3个ViewPager页面
        for (int i = 1; i <= titles.length; i++) {
            ViewPagerHomeItemViewModel itemViewModel = new ViewPagerHomeItemViewModel(this, "第" + i + "个页面");
            items.add(itemViewModel);
        }
    }


    //给ViewPager添加PageTitle
    public final BindingViewPagerAdapter.PageTitles<ViewPagerHomeItemViewModel> pageTitles = new BindingViewPagerAdapter.PageTitles<ViewPagerHomeItemViewModel>() {
        @Override
        public CharSequence getPageTitle(int position, ViewPagerHomeItemViewModel item) {
            return titles[position];
        }
    };
    //给ViewPager添加Adpter，请使用自定义的Adapter继承BindingViewPagerAdapter，重写onBindBinding方法
    public final ViewPagerHomeBindingAdapter adapter = new ViewPagerHomeBindingAdapter();
    //ViewPager切换监听
    public BindingCommand<Integer> onPageSelectedCommand = new BindingCommand<>(new BindingConsumer<Integer>() {
        @Override
        public void call(Integer index) {
            ToastUtils.showShort("ViewPager切换：" + index);
        }
    });
    

    public BindingCommand LeftOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            //采用ARouter+RxBus实现组件间通信
            RxBus.getDefault().post(EventConstants.LEFT);
        }
    });
}
